package com.guanyong.webapi.controllers.api;

import com.guanyong.model.MessageModel;
import com.guanyong.model.MessageReadlogModel;
import com.guanyong.model.UserBaseModel;
import com.guanyong.services.MessageReadlogService;
import com.guanyong.services.MessageService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.handle.UserHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by mxk on 2017/6/6.
 */
@RestController
@RequestMapping("/api/message/")
public class MeeageApi {
    @Autowired
    MessageService messageService;

    @Autowired
    MessageReadlogService messageReadlogService;

    @Autowired
    UserHandle userHandle;

    @RequestMapping(value = "getusermessagelist", method = RequestMethod.GET)
    ResultObject<HashMap<String, Object>> getMessageList(HttpServletRequest request,
                                                         @RequestParam int pno,
                                                         @RequestParam int pagesize,
                                                         @RequestParam int allcount) throws
            ParseException {
        ResultObject<HashMap<String, Object>> result = new ResultObject<HashMap<String, Object>>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            HashMap<String, Object> hashMap =
                    messageService.GetUserMessageList(userBaseModel.getfId(),
                            userBaseModel.getfIntime(), pno,
                    pagesize, allcount);
            result.setResult(hashMap);
        }
        else{
            result.setCode(ResultCode.NoLogin);
        }
        return result;
    }

    @RequestMapping(value = "getnewmessage", method = RequestMethod.POST)
    ResultObject<Boolean>  GetNewMessage(HttpServletRequest request) {
        ResultObject<Boolean> resultObject = new ResultObject<Boolean>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            Boolean aBoolean=messageService.GetNewMessage(userBaseModel.getfId(),
                    userBaseModel.getfIntime());
            resultObject.setResult(aBoolean);
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }


    @RequestMapping(value = "readmessage", method = RequestMethod.POST)
    ResultObject<Integer> AddMessageReadLog(HttpServletRequest request,
                                            @RequestParam int msgid) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            int res = messageReadlogService.AddMessageReadlog(userBaseModel.getfId(), msgid);
            if (res > -1) {
                resultObject.setCode(ResultCode.OperateOk);
            } else {
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }

    @RequestMapping(value = "delreadmessage", method = RequestMethod.POST)
    ResultObject<Integer> UpdateMessageReadLog(HttpServletRequest request,
                                               @RequestParam int msgid) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            MessageReadlogModel messageReadlogModel = messageReadlogService.SelectMessageReadlog
                    (userBaseModel.getfId(), msgid);
            if (messageReadlogModel != null) {
                int res = messageReadlogService.UpdateMessageReadlog(messageReadlogModel.getfId(), 1);

                if (res > -1) {
                    resultObject.setCode(ResultCode.OperateOk);
                } else {
                    resultObject.setCode(ResultCode.OperateFail);
                }
            } else {
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }
}
